package com.xumz.service.crawler;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Boke {
	private String connectUrl;
	private Document doc;
	private Pattern pattern;
	private Matcher matcher;
	// 格式：随笔 文章 评论 【引用】 顺序
	
	private String regNumber = "\\d*";

	// 格式一：随笔- 26 文章- 0 评论- 51
//	private String regSuibiNum = "随笔- \\d*";
//	private String regWenzhangNum = "文章- \\d*";
//	private String regPinglunNum = "评论- \\d*";
	// 格式二：随笔 - 4, 文章 - 0, 评论 - 8, 引用 - 0
	// 格式三：随笔 - 4 文章 - 0 评论 - 12
	private String regRead = "阅读:\\d*";
	private String regComment = "评论:\\d*";
//	private String regNum = "\\d*";
	private String bokeInfo;
	private int ownerLength;
	private List<String> bokeInfoList;
	private List<String> bokeInfoNumList;
	private String titleElementsByClass = "postTitl2";// 博客文章标题网站节点
	private String infoElementsByClass = "postDesc2";// 博客文章性信息网站节点
	private int dateLength=16;

	/**
	 * 链接博客主页
	 * 
	 * @param connectUrl
	 */
	public Boke(String connectUrl) {
		try {
			this.doc = Jsoup.connect(connectUrl).get();
//			System.out.println(doc);
			bokeInfo();
			bokeOwner();
			bookInfoList();
			allNum();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 获得博主
	 * 
	 * @return String
	 */
	public String bokeOwner() {
		String title = doc.getElementsByTag("title").text();
		String owner=title.substring(7, title.length() - 6);
		this.ownerLength=owner.length();
		return owner;
	}

	public String bokeInfo() {
		bokeInfo = doc.getElementById("blog_stats").text();
		return bokeInfo;
	}
	// 格式：随笔 文章 评论 【引用】 顺序
	public List<String> allNum() {
		pattern = Pattern.compile(this.regNumber);
		matcher = pattern.matcher(this.bokeInfo);
		bokeInfoNumList = new ArrayList<String>();
		String string = "";
		while (matcher.find()) {
			string = matcher.group();
			if (!string.equals("")) {
				bokeInfoNumList.add(string);
			}
		}
		return bokeInfoNumList;
	}

	public String bokeSuibiNum() {
		return this.bokeInfoNumList.get(0);

	}

	public String bokewenzhangNum() {
		return bokeInfoNumList.get(1);

	}

	public String bokepinglunNum() {
		return bokeInfoNumList.get(2);

	}

	/**
	 * 返回博客页所有文章标题
	 * 
	 * @return List<String>
	 */
	public List<String> bokeTitleList() {
		List<String> urlList = new ArrayList<String>();
		Elements titleListClass = doc.getElementsByClass(titleElementsByClass);
		for (Element vo : titleListClass) {
			urlList.add(vo.getElementsByTag("a").text());
		}
		return urlList;
	}

	/**
	 * 返回博客页所有文章链接
	 * 
	 * @return List<String>
	 */
	public List<String> bokeUrlList() {
		List<String> urlList = new ArrayList<String>();
		Elements urlListClass = doc.getElementsByClass(titleElementsByClass);
		for (Element vo : urlListClass) {
			Elements link = vo.getElementsByTag("a");
			urlList.add(link.attr("href"));
		}
		return urlList;
	}

	/**
	 * 返回博客页所有文章信息
	 * 
	 * @return List<String>
	 */
	public List<String> bookInfoList() {
		List<String> readInfoList = new ArrayList<String>();
		Elements elementList = doc.getElementsByClass(infoElementsByClass);
		for (Element vo : elementList) {
			readInfoList.add(vo.text());
		}
		this.bokeInfoList = readInfoList;
		return readInfoList;
	}

	/**
	 * 返回博客页所有文章更新日期
	 * 
	 * @return List<String>
	 */
	public List<String> bookTimeList() {
		List<String> timeList = new ArrayList<String>();
		for (String vo : this.bokeInfoList) {
			timeList.add(vo.substring(this.ownerLength+1, this.ownerLength+dateLength+1));// 日期
		}
		return timeList;
	}

	/**
	 * 返回博客页所有文章阅读数
	 * 
	 * @return List<String>
	 */
	public List<String> bookReadNumList() {
		List<String> readNumList = new ArrayList<String>();
		for (String vo : this.bokeInfoList) {
			System.out.println(vo);
			pattern = Pattern.compile(this.regRead);
			matcher = pattern.matcher(vo);
			while(matcher.find()){
				String readString = matcher.group();// 字符串格式：评论(**)
				readNumList.add(readString.substring(3));
			}
		}
		return readNumList;
	}

	/**
	 * 返回博客页所有文章评论数
	 * 
	 * @return List<String>
	 */
	public List<String> bookCommentNumList() {
		List<String> commentNumList = new ArrayList<String>();
		for (String vo : this.bokeInfoList) {
			pattern = Pattern.compile(this.regComment);
			matcher = pattern.matcher(vo);
			while (matcher.find()) {
				String commentString = matcher.group();// 字符串格式：评论(**)
				commentNumList.add(commentString.substring(3));
			}
		}
		return commentNumList;
	}

	public String getConnectUrl() {
		return connectUrl;
	}

	public void setConnectUrl(String connectUrl) {
		this.connectUrl = connectUrl;
	}

}
